package java44th.group.controller.admin.category;

import java44th.group.bean.admin.bo.cotegory.DeleteBo;
import java44th.group.bean.admin.bo.cotegory.UpdateBo;
import java44th.group.bean.po.MarketCategory;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.category.CreateVo;
import java44th.group.bean.admin.vo.category.L1TypeVo;
import java44th.group.bean.admin.vo.category.ListVo;
import java44th.group.service.admin.category.CategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Aurhor: 11437
 * @DATE: 2022/10/12 16:23
 * @Description:商品类别
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;


    //查询商品类别
    // @RequiresPermissions(value={"admin:category:create","*"},logical= Logical.OR)
    @GetMapping("list")
    public BaseRespVo list() {


        ListVo listVo = null;
        try {
            listVo = categoryService.categoryList();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }

        return BaseRespVo.ok(listVo);

    }

    //新建商品类别
    @RequiresPermissions(value={"admin:category:create","*"},logical= Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketCategory marketCategory) {
        if (marketCategory.getLevel().equals("L2") && marketCategory.getPid() == 0) {

            return BaseRespVo.falseParameter();
        }

        CreateVo createVo = null;
        try {
            createVo = categoryService.createCategory(marketCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }

        return BaseRespVo.ok(createVo);
    }

    //查类别L1的商品类
    @GetMapping("l1")
    public BaseRespVo l1() {

        L1TypeVo l1TypeVo = null;
        try {
            l1TypeVo = categoryService.selectTypeL1();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }


        return BaseRespVo.ok(l1TypeVo);
    }

    //修改商品类别
    @RequiresPermissions(value={"admin:category:update","*"},logical= Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateBo updateBo) {

        if (updateBo.getLevel().equals("L2") && updateBo.getPid() == 0) {

            return BaseRespVo.falseParameter();
        }

        try {
            categoryService.updateCategory(updateBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }

        return BaseRespVo.ok(null);

    }

    // 删除商品类别
    @RequiresPermissions(value={"admin:category:delete","*"},logical= Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody DeleteBo deleteBo) {


        try {
            categoryService.deleteCategory(deleteBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }

        return BaseRespVo.ok(null);
    }


}
